﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Quaack.Properties;

using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Drawing;
using System.ComponentModel;

//versie 6 jul 2014 14:40
namespace Quaack
{
    public partial class EigenPagina : System.Web.UI.Page
    {

        protected void page_preinit()
        {
            if (User.Identity.IsAuthenticated) Page.MasterPageFile = "~/Site-Ingelogd.Master";
            else
            {
                Page.MasterPageFile = "~/Site.Master";
                string url;
                url = "~/default.aspx";
                Response.Redirect(url);
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                String user = User.Identity.Name;
                int userID = 0;
                int huidigesender = 0;
                SqlConnection sqlConn = new SqlConnection(Settings.Default.quaackdb);

                /*   Profileid opvragen van de ingelogde gebruiker   */
                SqlCommand GetUserID = new SqlCommand("SELECT profileid from profile where username = '" + user + "'", sqlConn);


                /*   open database, haal de userID op en sluit database   */
                sqlConn.Open();
                object sqlObject = GetUserID.ExecuteScalar();
                if (sqlObject != null)
                {
                    // userID zal uiteindelijk gebruikt worden om de sender aan te geven in de message tabel
                    userID = Convert.ToInt32(sqlObject.ToString());
                    // Session["userID"] = userID;
                    huidigesender = userID;
                    Session.Add("userID", userID);
                    Session["userID"] = userID;
                    Session["user"] = user;

                }
                sqlConn.Close();

            }
            else
            {
                string url;
                url = "~/default.aspx";
                Response.Redirect(url);
            }
        }


        //dit kan nog toegevoegd in de asp gridview1  voor correcte datum
        //<asp:BoundField DataField="messdatetime" HeaderText="messdatetime" SortExpression="messdatetime" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="False"/>
        //


        // nog een extra ccsClass togevoegd om de sender en de parentmessid te verbergen met CSS in de aspx
        // <asp:BoundField DataField="sender" SortExpression="sender" HeaderText="pblocked" >
        //<ItemStyle CssClass="verberg" />
        //         </asp:BoundField>
        //        <asp:BoundField DataField="parentmessid" SortExpression="parentmessid" >

        //         <ItemStyle CssClass="verberg" />
        //         </asp:BoundField>

        //   Tevens in de CSS onder /content/Site.css
        //        .verberg {
        //    position: absolute;
        //    left: -999em;
        //    display: none;
        //      }



        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // hiermee controleren we of we te maken hebben met een reactie op een bericht, zo ja komt in de 1ste kolom de text reactie
            // tevens wil ik de reageer functie uitzetten op eigen berichten volgens mij is dit wenselijk
            // Check if row is data row

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Get value of fift column. Index is zero based, dit is de perrantmessid
                // to get text of fift column we use Cells[6].Text 


                if (string.IsNullOrWhiteSpace(e.Row.Cells[6].Text))
                {
                    e.Row.Cells[0].Text = "";

                }
                else
                {
                    // als de cel een "&nbsp;" waarde heeft gaat de conversie mis, de "&nbsp;" vervang ik voor "0"
                    int CellValue = Convert.ToInt32(e.Row.Cells[6].Text.Replace("&nbsp;", "0"));

                    // If value is 1 then change to Yes, otherwise change to No
                    if (CellValue > 0)
                    {
                        e.Row.Cells[0].Text = "Reactie";
                    }
                }

            }
        }

        protected void GridView1_RowCommand(object zender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GaNaarGebruiker")
            {

                //string parentmessid = GridView1.SelectedRow.Cells[6].Text.Replace("&nbsp;", "0");
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");


                //string sender = GridView1.SelectedRow.Cells[5].Text;
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");



                //string sender = GridView1.SelectedRow.Cells[2].Text;
                string reageerder = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[2].Text.Replace("&nbsp;", "0");
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                Session["reageerder"] = reageerder;

                string url;
                url = "~/PaginaLezenAndereGebruiker.aspx";
                Response.Redirect(url);
            }

            if (e.CommandName == "Reageer")
            {

                int redirectvaneigenpagina = 1;
                string messid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[7].Text.Replace("&nbsp;", "0");
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");
                Session["messid"] = messid;
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                Session["redirectvaneigenpagina"] = redirectvaneigenpagina;

                string url;
                url = "~/Reageer.aspx";
                Response.Redirect(url);

            }

            if (e.CommandName == "Blokkeer")
            {

                // string parentmessid = GridView1.SelectedRow.Cells[5].Text.Replace("&nbsp;", "0");
                // string sender = GridView1.SelectedRow.Cells[4].Text;
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;
                string url;
                url = "~/BlokkeerDeBlokkeerVolger.aspx";
                Response.Redirect(url);

            }
            if (e.CommandName == "Verwijder")
            {
                SqlConnection sqlConn = new SqlConnection(Settings.Default.quaackdb);
                int mmessid = 0;
                int pmessid = 0;
                int msender = 0;
                int userID = 0;

                // string parentmessid = GridView1.SelectedRow.Cells[5].Text.Replace("&nbsp;", "0");
                // string sender = GridView1.SelectedRow.Cells[4].Text;
                string messid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[7].Text.Replace("&nbsp;", "0");
                string parentmessid = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");
                string sender = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");
                Session["messid"] = messid;
                Session["parentmessid"] = parentmessid;
                Session["sender"] = sender;

                mmessid = Convert.ToInt32(messid);
                pmessid = Convert.ToInt32(parentmessid);
                msender = Convert.ToInt32(sender);
                userID = Convert.ToInt32(Session["userID"]);
              

                if (pmessid == 0) // check of het bericht een hoofdbercht is zo ja zet de reomved bit van alle reacties van het hoofdbericht op 1
                {

                    sqlConn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = sqlConn;
                    cmd.CommandText = "UPDATE message SET removed = 1, removedby = " + userID + " where messid = " + mmessid + "OR parentmessid  = " + mmessid + "" ;
                    cmd.ExecuteReader();
                    sqlConn.Close();
                   // MessageBox.Show(new Form() { TopMost = true }, "De berichten zijn verwijderd!");
                    //Server.Transfer("Default.aspx");
                    string url;
                    url = "~/EigenPagina.aspx";
                    Response.Redirect(url);

                }
                else if (pmessid > 0) // check of het bericht een reactie is zo ja zet de removed bit van de reacties op 1
                {

                    sqlConn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = sqlConn;
                    cmd.CommandText = "UPDATE message SET removed = 1 where messid = " + mmessid + "";
                    cmd.ExecuteReader();
                    sqlConn.Close();
                  //  MessageBox.Show(new Form() { TopMost = true }, "Het bericht is verwijderd!");
                    //Server.Transfer("Default.aspx");
                    string url;
                    url = "~/EigenPagina.aspx";
                    Response.Redirect(url);
                }

                //string url;
                //url = "~/VerwijderBericht.aspx";
                //Response.Redirect(url);

            }
        }

        protected void GridView2_RowCommand(object zender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "GaNaarGebruiker")
            {

                //string parentmessid = GridView2.SelectedRow.Cells[6].Text.Replace("&nbsp;", "0");
               // string parentmessid = GridView2.Rows[int.Parse(e.CommandArgument.ToString())].Cells[6].Text.Replace("&nbsp;", "0");


                //string sender = GridView2.SelectedRow.Cells[5].Text;
                //string sender = GridView2.Rows[int.Parse(e.CommandArgument.ToString())].Cells[5].Text.Replace("&nbsp;", "0");



                //string sender = GridView2.SelectedRow.Cells[2].Text;
                string reageerder = GridView2.Rows[int.Parse(e.CommandArgument.ToString())].Cells[1].Text.Replace("&nbsp;", "0");
               // Session["parentmessid"] = parentmessid;
                //Session["sender"] = sender;
                Session["reageerder"] = reageerder;

                string url;
                url = "~/PaginaLezenAndereGebruiker.aspx";
                Response.Redirect(url);
            }

           



        }


    }
}




